//var basePath = "http://127.0.0.1/GraduationProject";
var total = 0;
$(function (){
	
	dataStyle = sessionStorage.getItem("dataStyle");
	
	if(JSON.stringify(dataStyle) =="{}" || JSON.stringify(dataStyle) == null || dataStyle == ""){
		loadGoodsInfo();
	}else{
		loadGoodsInfo(dataStyle);
	}
	
	clickGoods();
	
	//加载商品属性
	goodsProperty();
	
	sessionStorage.setItem("dataStyle","");
	
});

/**
 * 加载商品信息
 */
function loadGoodsInfo(obj){
	var params = {};
	
	params.size = 170;
	params.current = 1;
	if(typeof obj == "string"){
		$("#position").html(obj);
		params.condition = {};
		params.condition.suitAge = obj;
		
	}else if(typeof obj == "object"){
		$("#position").html("所有商品");
		$(obj).parent().addClass("on").siblings().removeClass("on");
		var style = $(obj).attr("data-style");
		params.condition = {};
		params.condition[style]= $(obj).html();
	}
	
	
	$.ajax({
		type:"get",
		url:basePath + "/goods/list",
		data:{"page":JSON.stringify(params)},
		async:false,
		success:function(data){
			if(data){
				var data = data.obj.records;
				total = Math.ceil(data.length/12);
				
				var $ul = $("#goodsListUl");
				$ul.html("");
				for(var i = 0; i< 12 ;i++){
					var images = data[i].images.split(",");
					var $li = $("<li class='goodsListLi boxShadow' data-id='"+data[i].id+"'></li>");
					var $a = $("<a href='commodityDetail.html?goodsId="+data[i].id+"'></a>");
					var $divImg = $("<div class='imgLi'><img src='"+images[0]+"'></div>");
					var $divText = $("<div class='gl_text'></div>");
					var $pDesc = $("<p class='description'>"+data[i].goodsName+"</p>");
					var $pPrice = $("<p class='price'>"+data[i].price+"<span style='font-size:12px;'> 艾乐币</span></p>");
					$li.append($a);
					$a.append($divImg);
					$a.append($divText);
					$divText.append($pDesc);
					$divText.append($pPrice);
					$ul.append($li);
				}
				
				pagination(obj);
			}
		}
	});
	
}

/**
 * 分页
 */
function pagination(obj){
	var params = {};
	params.size = 12;
	params.current = 1;
	if(typeof obj == "string"){
		$("#position").html(obj);
		params.condition = {};
		params.condition.suitAge = obj;
		
	}else if(typeof obj == "object"){
		$("#position").html("所有商品");
		$(obj).parent().addClass("on").siblings().removeClass("on");
		var style = $(obj).attr("data-style");
		params.condition = {};
		params.condition[style]= $(obj).html();
	}
	
	$('#pageLimit').bootstrapPaginator({
		currentPage: 1,//当前的请求页面。
		totalPages: total,//一共多少页。
		size:"normal",//应该是页眉的大小。
		bootstrapMajorVersion: 3,//bootstrap的版本要求。
		alignment:"center",
		numberOfPages:12,//一页列出多少数据。
		itemTexts: function (type, page, current) {//如下的代码是将页眉显示的中文显示我们自定义的中文。
			switch (type) {
				case "first": return "首页";
				case "prev": return "上一页";
				case "next": return "下一页";
				case "last": return "末页";
				case "page": return page;
			}
		},
		onPageClicked: function (event, originalEvent, type, page){//给每个页眉绑定一个事件，其实就是ajax请求，其中page变量为当前点击的页上的数字。
			params.current = page;
			$.ajax({
				type:"get",
				url:basePath + "/goods/list",
				data:{"page":JSON.stringify(params)},
				async:true,
				success:function(data){
					if(data){
						var data = data.obj.records;
						var $ul = $("#goodsListUl");
						$ul.html("");
						for(var i in data){
							var images = data[i].images.split(",");
							var $li = $("<li class='goodsListLi boxShadow' data-id='"+data[i].id+"'></li>");
							var $a = $("<a href='commodityDetail.html?goodsId="+data[i].id+"'></a>");
							var $divImg = $("<div class='imgLi'><img src='"+images[0]+"'></div>");
							var $divText = $("<div class='gl_text'></div>");
							var $pDesc = $("<p class='description'>"+data[i].goodsName+"</p>");
							var $pPrice = $("<p class='price'>￥"+data[i].price+"</p>");
							$li.append($a);
							$a.append($divImg);
							$a.append($divText);
							$divText.append($pDesc);
							$divText.append($pPrice);
							$ul.append($li);
						}
					}
				}
			});
		}
	});	
}

/**
 * 全部商品
 */
function allGoods(){
	$("#position").html("所有商品");
	window.location.href = "allCommodity.html";
}

/**
 * 商品点击事件
 */
function clickGoods(){
	$(".goodsListLi a").on("click",function(){
		var goodId = $(this).attr("data-id");
		hrefDetailPage(goodId);
	});
}

/**
 * 点击商品跳转到详情页
 */
function hrefDetailPage(goodId){
	window.location.href="commodityDetail.html?goodsId="+goodId;
}

/**
 * 商品属性
 */
function goodsProperty(){
	$.ajax({
		type:"get",
		url:basePath + "/goods/listGoodsProperties",
		async:true,
		success:function(data){
			if(data){
				var data = data.obj;
				var fixedWay = data.fixedWay;
				var installWay = data.installWay;
				var seatStyle = data.seatStyle;
				var suitAge = data.suitAge;
				
				var $ul = $("#suitAge ul");
				for(var i = 0;i< 5;i++){
					var $li = $("<li><span onclick='loadGoodsInfo(this)' data-style='suitAge'>" + suitAge[i] + "</span></li>");
					$ul.append($li);
				}
				
				var $ul = $("#fixedWay ul");
				for(var i = 0;i< 5;i++){
					var $li = $("<li><span onclick='loadGoodsInfo(this)' data-style='fixedWay'>" + fixedWay[i] + "</span></li>");
					$ul.append($li);
				}
				
				var $ul = $("#installWay ul");
				for(var i = 0;i<4;i++){
					var $li = $("<li><span onclick='loadGoodsInfo(this)' data-style='installWay'>" + installWay[i] + "</span></li>");
					$ul.append($li);
				}
				
				var $ul = $("#seatStyle ul");
				for(var i = 0;i<8;i++){
					var $li = $("<li><span onclick='loadGoodsInfo(this)' data-style='seatStyle'>" + seatStyle[i] + "</span></li>");
					$ul.append($li);
				}
				
			}
		}
	});
}






